package com.shuiyun.chat.service;

import com.shuiyun.chat.entity.bo.SendGroupMessageBO;
import com.shuiyun.chat.entity.bo.SendMsgBO;
import com.shuiyun.chat.entity.message.SendGroupMessage;
import com.shuiyun.chat.entity.message.SimpleChatMessage;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shuiyun
 * @program chatServer
 * @description 聊天服务类
 * @create 2021-05-14 16:30
 **/
@Service
public class ChatService {

    @Autowired
    private ClientService clientService;

    /**
     *简单消息发送
     * @param bo 消息数据
     * @Author: shuiyun
     * @Date: 2021/5/13 0:28
     */
    public void sendMsg(SendMsgBO bo) {
        Channel channel = clientService.getChannel(bo.getMyUserName());
        if (null == channel){
            throw new RuntimeException("你已离线");
        }
        channel.writeAndFlush(new SimpleChatMessage(bo.getMyUserName(),bo.getUserName(),bo.getMessage()));
    }

    public void sendGroupMessage(SendGroupMessageBO bo) {
        Channel channel = clientService.getChannel(bo.getUserName());
        if (null == channel){
            throw new RuntimeException("你已离线");
        }
        channel.writeAndFlush(new SendGroupMessage(bo.getGroupId(),bo.getUserName(),bo.getMessage()));
    }
}
